/**
 * 服务相关API接口
 * @module api/service
 * @author 互成一家
 * @description 服务模块相关接口请求函数
 */
import request from './request';
import { USE_MOCK } from './config';
import { 
  mockGetServiceCategories, 
  mockGetServiceList, 
  mockGetServiceDetail, 
  mockGetServiceReviews,
  mockSubmitServiceReview
} from '../mock/service';

/**
 * 获取服务分类列表
 * @function getServiceCategories
 * @returns {Promise} 返回Promise对象
 */
export const getServiceCategories = () => {
  if (USE_MOCK) {
    return mockGetServiceCategories();
  }
  return request.get('/api/services/categories');
};

/**
 * 获取服务列表
 * @function getServiceList
 * @param {Object} params - 查询参数
 * @param {Number|String} [params.categoryId] - 分类ID
 * @param {String} [params.keyword] - 搜索关键词
 * @param {Number} [params.page=1] - 页码
 * @param {Number} [params.pageSize=10] - 每页条数
 * @returns {Promise} 返回Promise对象
 */
export const getServiceList = (params) => {
  if (USE_MOCK) {
    return mockGetServiceList(params);
  }
  return request.get('/api/services', params);
};

/**
 * 获取服务详情
 * @function getServiceDetail
 * @param {Number|String} serviceId - 服务ID
 * @returns {Promise} 返回Promise对象
 */
export const getServiceDetail = (serviceId) => {
  if (USE_MOCK) {
    return mockGetServiceDetail(serviceId);
  }
  return request.get(`/api/services/${serviceId}`);
};

/**
 * 获取服务评价列表
 * @function getServiceReviews
 * @param {Object} params - 查询参数
 * @param {Number|String} params.serviceId - 服务ID
 * @param {Number} [params.rating] - 评分筛选：0=全部，4=好评，3=中评，1=差评
 * @param {Boolean} [params.hasImage] - 是否有图片
 * @param {Number} [params.page=1] - 页码
 * @param {Number} [params.pageSize=10] - 每页条数
 * @returns {Promise} 返回Promise对象
 */
export const getServiceReviews = (params) => {
  if (USE_MOCK) {
    return mockGetServiceReviews(params);
  }
  return request.get('/api/services/reviews', params);
};

/**
 * 提交服务评价
 * @function submitServiceReview
 * @param {Object} reviewData - 评价数据
 * @param {Number|String} reviewData.serviceId - 服务ID
 * @param {Number} reviewData.rating - 评分(1-5)
 * @param {String} reviewData.content - 评价内容
 * @param {Array} [reviewData.images] - 图片列表
 * @returns {Promise} 返回Promise对象
 */
export const submitServiceReview = (reviewData) => {
  if (USE_MOCK) {
    return mockSubmitServiceReview(reviewData);
  }
  return request.post('/api/services/reviews', reviewData);
};

/**
 * 收藏服务
 * @function favoriteService
 * @param {Number|String} serviceId - 服务ID
 * @returns {Promise} 返回Promise对象
 */
export const favoriteService = (serviceId) => {
  if (USE_MOCK) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '收藏成功',
          data: { success: true }
        });
      }, 500);
    });
  }
  return request.post(`/api/services/${serviceId}/favorite`);
};

/**
 * 取消收藏服务
 * @function unfavoriteService
 * @param {Number|String} serviceId - 服务ID
 * @returns {Promise} 返回Promise对象
 */
export const unfavoriteService = (serviceId) => {
  if (USE_MOCK) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '取消收藏成功',
          data: { success: true }
        });
      }, 500);
    });
  }
  return request.delete(`/api/services/${serviceId}/favorite`);
};

/**
 * 预约服务
 * @function bookService
 * @param {Object} bookingData - 预约数据
 * @param {Number|String} bookingData.serviceId - 服务ID
 * @param {Number|String} bookingData.optionId - 选项ID
 * @param {String} bookingData.date - 预约日期，格式：YYYY-MM-DD
 * @param {String} bookingData.time - 预约时间，格式：HH:MM
 * @param {Object} bookingData.address - 服务地址
 * @param {String} bookingData.remark - 备注
 * @returns {Promise} 返回Promise对象
 */
export const bookService = (bookingData) => {
  if (USE_MOCK) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '预约成功',
          data: {
            success: true,
            bookingId: Date.now(),
            ...bookingData
          }
        });
      }, 800);
    });
  }
  return request.post('/api/services/booking', bookingData);
};
